package com.infosys.newSimpleInvertedIndex.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.infosys.newSimpleInvertedIndex.dto.EmployeeDTOI;
import com.infosys.newSimpleInvertedIndex.dto.EmployeeDTOO;
import com.infosys.newSimpleInvertedIndex.entities.Employee;
import com.infosys.newSimpleInvertedIndex.enums.DepartmentListEnum;
import com.infosys.newSimpleInvertedIndex.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.sql.Date;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 员工总表 前端控制器
 * </p>
 *
 * @author Tester
 * @since 2024-06-24
 */
@Controller
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;

    // crud
    // create
    @PostMapping("/addEmployee")
    public ResponseEntity<EmployeeDTOI> addEmployee(String name, String govId, DepartmentListEnum department, Date enrollDate) {
        EmployeeDTOI employeeDTOI = new EmployeeDTOI();
        employeeDTOI.setGovId(govId);
        employeeDTOI.setName(name);
        employeeDTOI.setDepartment(department);
        employeeDTOI.setEnrollDate(enrollDate);
        Employee employee = new Employee(employeeDTOI);

        boolean result = employeeService.save(employee);
        if (result) {
            return ResponseEntity.ok(employeeDTOI);
        } else {
            return ResponseEntity.badRequest().build();
        }
    }

    // update
    @PutMapping("/updateEmployee")
    public ResponseEntity<EmployeeDTOO> updateEmployee(String govId, String name, DepartmentListEnum department, LocalDate enrollDate) {
        EmployeeDTOI employeeDTOI = new EmployeeDTOI();
        employeeDTOI.setGovId(govId);
        employeeDTOI.setName(name);
        employeeDTOI.setDepartment(department);
        employeeDTOI.setEnrollDate(Date.valueOf(enrollDate));
        Employee employee = new Employee(employeeDTOI);
        EmployeeDTOO employeeDTOO = new EmployeeDTOO(employee);
        boolean result = employeeService.updateById(employee);
        if (result) {
            return ResponseEntity.ok(employeeDTOO);
        } else {
            return ResponseEntity.badRequest().build();
        }
    }

    // delete
    @DeleteMapping("/deleteEmployee")
    public ResponseEntity<EmployeeDTOO> deleteEmployee(String id) {
        Employee employee = employeeService.getById(id);
        if (employee == null) {
            return ResponseEntity.notFound().build();
        }
        boolean result = employeeService.removeById(id);
        EmployeeDTOO employeeDTOO = new EmployeeDTOO(employee);
        if (result) {
            return ResponseEntity.ok(employeeDTOO);
        } else {
            return ResponseEntity.badRequest().build();
        }
    }

    @GetMapping("/getFullEmployeeById")
    public ResponseEntity<Employee> getFullEmployeeById(String id) {
        Employee employee = employeeService.getEmployeeById(id);
        if (employee != null) {
            return ResponseEntity.ok(employee);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    // get
    @GetMapping("/getEmployeeById")
    public ResponseEntity<EmployeeDTOO> getEmployeeById(String id) {
        Employee employee = employeeService.getById(id);
        EmployeeDTOO employeeDTOO = new EmployeeDTOO(employee);
        if (employee != null) {
            return ResponseEntity.ok(employeeDTOO);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    @DeleteMapping("/batchDeleteEmployees")
    public ResponseEntity<String> batchDeleteEmployees(@RequestBody List<String> ids) {
        boolean result = employeeService.removeByIds(ids);
        if (result) {
            return ResponseEntity.ok("Delete successfully");
        } else {
            return ResponseEntity.badRequest().build();
        }
    }

    @GetMapping("/getByGovId")
    public ResponseEntity<EmployeeDTOO> getEmployeeByGovId(String govId) {
        Employee employee = employeeService.getOne(new QueryWrapper<Employee>().eq("gov_id", govId));
        EmployeeDTOO employeeDTOO = new EmployeeDTOO(employee);
        if (employee != null) {
            return ResponseEntity.ok(employeeDTOO);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    @GetMapping("/getByName")
    public ResponseEntity<List<EmployeeDTOO>> getEmployeeByName(String name) {
        List<Employee> employees = employeeService.list(new QueryWrapper<Employee>().eq("name", name));
        List<EmployeeDTOO> employeeDTOOs = new ArrayList<>();
        for (Employee employee : employees) {
            employeeDTOOs.add(new EmployeeDTOO(employee));
        }
        if (employees != null) {
            return ResponseEntity.ok(employeeDTOOs);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    @GetMapping("/getByDeptName")
    public ResponseEntity<List<EmployeeDTOO>> getEmployeeByDeptName(String department) {
        List<Employee> employees = employeeService.list(new QueryWrapper<Employee>().eq("department", department));
        List<EmployeeDTOO> employeeDTOOs = new ArrayList<>();
        for (Employee employee : employees) {
            employeeDTOOs.add(new EmployeeDTOO(employee));
        }
        if (employees != null) {
            return ResponseEntity.ok(employeeDTOOs);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    @GetMapping("/getByEnrollDate")
    public ResponseEntity<List<EmployeeDTOO>> getEmployeeByEnrollDate(LocalDate enrollDateStart, LocalDate enrollDateEnd) {
        QueryWrapper<Employee> queryWrapper = new QueryWrapper<>();
        queryWrapper.between("enroll_date", enrollDateStart.toString(), enrollDateEnd.toString());
        // queryWrapper.gt("enroll_date", enrollDateStart).lt("enroll_date", enrollDateEnd);
        List<Employee> employees = employeeService.list(queryWrapper);
        List<EmployeeDTOO> employeeDTOOs = new ArrayList<>();
        for (Employee employee : employees) {
            employeeDTOOs.add(new EmployeeDTOO(employee));
        }
        if (employees != null) {
            return ResponseEntity.ok(employeeDTOOs);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    @GetMapping("/getAll")
    public ResponseEntity<List<EmployeeDTOO>> getAllEmployee() {
        List<Employee> employees = employeeService.list();
        List<EmployeeDTOO> employeeDTOOs = new ArrayList<>();
        for (Employee employee : employees) {
            employeeDTOOs.add(new EmployeeDTOO(employee));
        }
        if (employees != null) {
            return ResponseEntity.ok(employeeDTOOs);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    @GetMapping("/getByAgeRange")
    public ResponseEntity<List<EmployeeDTOO>> getEmployeeByAgeRange(LocalDate startDate, LocalDate endDate) {
        List<Employee> employees = employeeService.getEmployeeByAgeRange(startDate.toString(), endDate.toString());

        List<EmployeeDTOO> employeeDTOOs = new ArrayList<>();
        for (Employee employee : employees) {
            employeeDTOOs.add(new EmployeeDTOO(employee));
        }
        if (employees != null) {
            return ResponseEntity.ok(employeeDTOOs);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

}
